package com.ychs.lawyer.util;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: clh
 * @Date: 2024/07/25/16:32
 * @Description:
 */

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * Jwt工具类
 *
 * @author ruoyi
 */
public class JwtUtils
{
    public static String secret = "abcdefghijklmnopqrstuvwxyz";

    /**
     * 从数据声明生成令牌
     *
     * @param claims 数据声明
     * @return 令牌
     */
    public static String createToken(Map<String, Object> claims){
        String token = Jwts.builder().
                setClaims(claims).
                signWith(SignatureAlgorithm.HS512, secret).compact();
        return token;
    }

    /**
     * 从令牌中获取数据声明
     *
     * @param token 令牌
     * @return 数据声明
     */
    public static Claims parseToken(String token)
    {
        return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
    }

    public static void main(String[] args) {
        Map claims = new HashMap();
        claims.put("name","admin");
        claims.put("age",123);

        String token = JwtUtils.createToken(claims);
        System.out.println(token);
        Claims claims1 = JwtUtils.parseToken(token);
        System.out.println(claims1.get("name"));
        System.out.println(claims1.get("age"));
    }
}
